package com.aierjun.notepadjun;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;



public class MainActivity extends HelperActivity {

    private DBhelper dBhelper;
    public boolean anniu=true;

    protected Button btn;
    protected EditText btn_zhanghao, btn_mima;
    protected TextView error,btn1;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.denglu_activity);
        findView();
        initView();

        if (anniu=true)
            dBhelper = new DBhelper(MainActivity.this, "Test.db", null, 1);
            SQLiteDatabase db = dBhelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("accounts", "aierjun");
            values.put("password", "378592175");
            db.insert("test", null, values);
    }



    public void findView() {
        btn = (Button) findViewById(R.id.btn);
        btn1= (TextView) findViewById(R.id.zhuce_btn);
        btn_zhanghao = (EditText) findViewById(R.id.user_et);
        btn_mima = (EditText) findViewById(R.id.password_et);
        error = (TextView) findViewById(R.id.error_message);
    }

    public void initView() {
            btn.setOnClickListener(onClickListener);
            btn1.setOnClickListener(onClickListener);
        }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.btn:
                    getshuju();
                   /* if ((zhanghao.equals(hao)&&mima.equals(mi))){
                    startActivity(new Intent(MainActivity.this, HistoryActivity.class));
                }else if (zhanghao.length()==0||mi.length()==0){
                        error.setVisibility(View.VISIBLE);
                    }
                else  error.setVisibility(View.VISIBLE);*/
                    break;
                case R.id.zhuce_btn:
                    startActivity(new Intent(MainActivity.this,HelperActivity.class));
                    break;
            }
        }
    };
    private String zhanghao;
    private String mima;
    private String hao;
    private String mi;

    public void getshuju(){
        dBhelper=new DBhelper(this,"Test.db",null,1);
        Button btn_go= (Button) findViewById(R.id.btn);
        btn_go.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase db = dBhelper.getWritableDatabase();
                zhanghao = btn_zhanghao.getText().toString();
                mima = btn_mima.getText().toString();
                Cursor cursor = db.query("test", null, null, null, null, null, null);
                if (zhanghao.length()==0||mima.length()==0){
                    error.setVisibility(View.VISIBLE);
                }while (cursor.moveToNext()) {
                    do {
                        hao = cursor.getString(cursor.getColumnIndex("accounts"));
                        mi = cursor.getString(cursor.getColumnIndex("password"));

                        Log.d("MainActivity", "user accounts is" + hao);
                        Log.d("MainActivity", "user accounts is" + mi);



                        if (zhanghao.equals(hao) && mima.equals(mi)) {
                            startActivity(new Intent(MainActivity.this, HistoryActivity.class));
                            error.setVisibility(View.INVISIBLE);
                        }

                    } while (cursor.moveToNext());
                }
            }
        });
    }
}
